package com.clz.appdesign.myapp.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.clz.appdesign.myapp.R;

/**
 * @author chenliangzhi
 * @date 2018/10/25
 * @describe 范围裁切和几何变换
 */

public class RangeCuttingView extends View {
    private Paint mPaint;
    private Context mContext;

    public RangeCuttingView(Context context) {
        super(context);
    }

    public RangeCuttingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        this.mContext = context;
    }

    public RangeCuttingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
       // canvas.save();
        Bitmap bitMap = BitmapFactory.decodeResource(getResources(), R.drawable.batman);
        //使用canvas.clipRect裁切
//        canvas.clipRect(100, 100, 800, 800);
//        canvas.drawBitmap(bitMap, 50, 50, mPaint);
//        canvas.restore();

        //使用clipPath裁切
//        Path path = new Path();
//        path.addCircle(400,400,200, Path.Direction.CCW);
//        canvas.save();
//        canvas.clipPath(path);
//        canvas.drawBitmap(bitMap,50,100,mPaint);
//        canvas.restore();

        //画布旋转
//        canvas.save();
//        canvas.rotate(45,200,200);
//        canvas.drawRect(100,100,400,300,mPaint);
//        canvas.restore();

        /**
         * 画布缩放
         * 参数里的 sx sy 是横向和纵向的放缩倍数； px py 是放缩的轴心。
         */
//        canvas.save();
//        canvas.scale(0.6f,0.6f,50+bitMap.getWidth()/2,50+bitMap.getHeight()/2);
//        canvas.drawBitmap(bitMap, 50, 50, mPaint);
//        canvas.restore();

        /**
         * 画布错切
         *  skew(float sx, float sy)
         * 参数里的 sx 和 sy 是 x 方向和 y 方向的错切系数。
         */
        canvas.save();
        canvas.scale(0.6f,0.6f,50+bitMap.getWidth()/2,50+bitMap.getHeight()/2);
        canvas.skew(0.2f,0);
        canvas.drawBitmap(bitMap, 50, 50, mPaint);
    }
}
